let OSS = require('ali-oss');
let that = this;
export default {
	data() {
		return {
			client: {},
			percentage: 0,
			errorFile: '',
			// videoFileTypes: [".mp4", ".avi", ".flv", ".wmv", ".m3u8"],
			// videoFileSize:0,
			// videoFileMaxSize:102400,
			// videoFilePath:''
			
		}
	},
	methods: {
		async createOss(Obj) {
			try {
				this.client = new OSS({
					bucket: 'ky-dj', //创建的bucket的名称
					region: 'oss-accelerate', //创建的时候，bucket所在的区域，华北2->oss-cn-beijing ；其他的可以去百度
					accessKeyId: Obj.accessKeyId, // 阿里云控制台创建的AccessKey
					accessKeySecret: Obj.accessKeySecret, //阿里云控制台创建的AccessSecret
					stsToken: Obj.securityToken
				})
			} catch (e) {
				console.log(e)
			}
		},
		// async put(ObjName, fileUrl){
		// 	try {
		// 		// 分片上传文件
		// 		let result = await this.client.multipartUpload(`${ObjName}`, fileUrl, {
		// 			progress: async function(p) {
		// 				this.percentage = p * 100;
		// 				// console.log(this.percentage);
		// 			}
		// 		})
		// 		return result
		// 	} catch (e) {
		// 		console.log(e)
		// 	}
		// },
		async signatureUrl(ObjName) {
			try {
				let result = await this.client.signatureUrl(`${ObjName}`)
				return result
			} catch (e) {
				console.log(e)
			}
		},
		getFileNameUUID() {
			const rx = (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
			return `${+new Date()}_${rx}${rx}`
		},
		getFileName(name) {
			return name.substring(name.lastIndexOf(".")); //.txt
		},
		//处理链接，预防链接带参数
		removeUrlParams(url) {
			if (url) {
				console.log('===处理链接===');
				const searchParams = new URLSearchParams(new URL(url).search);
				const cleanUrl = new URL(url);
				searchParams.forEach((value, key) => {
					cleanUrl.searchParams.delete(key);
				});
				return cleanUrl.toString();
			} else {
				return '';
			}
		},
		//检查图片类型和大小
		checkPicFile(file, MaxSize) {
			let fileTypes = [".jpg", ".jpeg", ".png"],
				fileSize = 0,
				fileMaxSize = (MaxSize || 1) * 1024, //最大多少M，不传默认1m
				filePath = file.name;
			//当括号里面的值为0、空字符、false 、null 、undefined的时候就相当于false
			console.log(filePath);
			if (filePath) {
				let isNext = false;
				let fileEnd = (filePath.substring(filePath.indexOf("."))).toLowerCase();
				for (let i = 0; i < fileTypes.length; i++) {
					if (fileTypes[i] == fileEnd) {
						isNext = true;
						break;
					}
				}
				if (!isNext) {
					this.$message({
						message: '不接受此文件类型，图片只支持jpg/jpeg、png格式',
						type: 'error',
						duration: 2500,
						onClose: () => {}
					})
					return false;
				}
				fileSize = file.size;
				let size = fileSize / 1024;
				if (size > fileMaxSize) {
					this.$message({
						message: `图片大小不能大于${(MaxSize||1)}M！`,
						type: 'error',
						duration: 1500,
						onClose: () => {}
					})
					return false;
				} else if (size <= 0) {
					this.$message({
						message: '图片大小不能为0M！',
						type: 'error',
						duration: 1500,
						onClose: () => {}
					})
					return false;
				}
				return true;
			} else {
				return false;
			}
		},
		//单视频文件上传---检查视频类型和大小
		checkVideoFile(file, MaxSize) {
			let fileTypes = [".mp4", ".avi", ".flv", ".wmv", ".m3u8"],
				fileSize = 0,
				fileMaxSize = (MaxSize || 100) * 1024, //最大多少M，不传默认100m
				filePath = file.name;
			//当括号里面的值为0、空字符、false 、null 、undefined的时候就相当于false
			console.log(filePath);
			if (filePath) {
				let isNext = false,
					fileEnd = (filePath.substring(filePath.indexOf("."))).toLowerCase();
				for (let i = 0; i < fileTypes.length; i++) {
					if (fileTypes[i] == fileEnd) {
						isNext = true;
						break;
					}
				}
				if (!isNext) {
					this.$message({
						message: '不接受此文件类型，视频只支持mp4、avi、flv、wmv、m3u8格式',
						type: 'error',
						duration: 2500,
						onClose: () => {}
					})
					return false;
				}
				fileSize = file.size;
				let size = fileSize / 1024;
				if (size > fileMaxSize) {
					this.$message({
						message: `视频大小不能大于${(MaxSize||1)}M！`,
						type: 'error',
						duration: 1500,
						onClose: () => {}
					})
					return false;
				} else if (size <= 0) {
					this.$message({
						message: '视频大小不能为0M！',
						type: 'error',
						duration: 1500,
						onClose: () => {}
					})
					return false;
				}
				return true;
			} else {
				return false;
			}
		},
		//批量上传---检查视频类型和大小
		async checkAllVideoFile(fileList, MaxSize) {
			let fileTypes = [".mp4", ".avi", ".flv", ".wmv", ".m3u8"],
				fileSize = 0,
				isNext = [],
				list = [],
				fileMaxSize = (MaxSize || 100) * 1024, //视频最大多少M，不传默认100m
				nameError = '',//视频类型错误
				exceedSize = '',//视频超过大小错误
				zeroSize = '';//视频大小为0错误
			console.log(fileList);
			let listLen = fileList.length;
			for (var i = 0; i < listLen; i++) {
				fileList[i].video_name = fileList[i].name;
				fileList[i].playlet_number = fileList[i].name.substring(0, fileList[i].name.lastIndexOf("."));
				if(fileList[i].name){
					let fileEnd = (fileList[i].name.substring(fileList[i].name.indexOf("."))).toLowerCase();
					if (!fileTypes.includes(fileEnd)) {
						isNext[i] = -1;
						nameError += (nameError ? ('、' + fileList[i].name) : fileList[i].name);
					} else {
						isNext[i] = 1;
					}
					fileSize = fileList[i].size;
					let size = fileSize / 1024;
					if(size > fileMaxSize){
						isNext[i] = -2;
						exceedSize += (exceedSize ? ('、' + fileList[i].name) : fileList[i].name);
					}else if (size <= 0) {
						isNext[i] = -3;
						zeroSize += (zeroSize ? ('、' + fileList[i].name) : fileList[i].name);
					}
				}
			}
			
				console.warn('isEndisEndisEnd');
				console.log(isNext);
				let nameisNext = isNext.indexOf(-1),nl = [];
				if(nameisNext>-1){
					await this.$message({
						message: `${nameError||''} 不接受此文件类型，视频只支持mp4、avi、flv、wmv、m3u8格式`,
						type: 'error',
						duration: 5000
					})
				}
				let exceedisNext = isNext.indexOf(-2),el = [];
				if(exceedisNext>-1){
					await this.$message({
						message: `${exceedSize||''} 视频大小不能大于${(MaxSize||1)}M！`,
						type: 'error',
						duration: 5000,
					})
				}
				let zeroisNext = isNext.indexOf(-3),zl = [];
				if(zeroisNext>-1){
					await this.$message({
						message: `${zeroSize||''} 视频大小不能为0M！`,
						type: 'error',
						duration: 5000
					})
				}
				let result = isNext.reduce((acc, curr, index) => {
				  if (curr === 1) {
				    acc.push(fileList[index]);
				  }
				  return acc;
				}, []);
				
				if(result.length){
					result.sort(function(a, b) {
						let nameA = a.name.toLowerCase();
						let nameB = b.name.toLowerCase();
						if (nameA < nameB) {
							return -1;
						}
						if (nameA > nameB) {
							return 1;
						}
						return 0;
					});
					result.sort(function(a, b) {
						let numA = parseInt(a.name.match(/\d+/)[0]);
						let numB = parseInt(b.name.match(/\d+/)[0]);
						return numA - numB;
					});
				}
				this.fileList = result;
		}
	},

}